/**
 * 
 */
package wt.stock.transform.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.Logger;

import wt.stock.data.SecurityQuoteData;
import wt.stock.data.Symbol;
import wt.stock.transform.ReadData;

/**
 * @author Wen Tao
 *
 */
public class ReadDataUtil {
	private Logger logger = Logger.getLogger(getClass());
	private ReadData readData = null;
	private String filePathRoot = "f:/wt/stock/data";
	
	public SecurityQuoteData readQuoteData(Symbol symbol){
		if(symbol==null || symbol.getSecurityCode()==null || symbol.getMarketCode()==null) return null;
		String filePath = filePathRoot;
		filePath = filePath+"/"+symbol.getMarketCode()+symbol.getSecurityCode()+".txt";
		
		try {
			FileInputStream is = new FileInputStream(filePath);
			SecurityQuoteData data = readData.readQuoteData(is);
			if(data.getSymbol()==null){
				data.setSymbol(symbol);
			}else{
				Symbol symbol2 = data.getSymbol();
				symbol2.setMerketCode(symbol.getMarketCode());
				symbol2.setMarketName(symbol.getMarketName());
			}
			return data;
		} catch (FileNotFoundException e) {
			logger.error("Data is not exist!", e);
		} catch (IOException e) {
			logger.error("Data file is something wrong! "+e.getMessage(), e);
		}
		return null;
	}

	public ReadData getReadData() {
		return readData;
	}

	public void setReadData(ReadData readData) {
		this.readData = readData;
	}

	public String getFilePathRoot() {
		return filePathRoot;
	}

	public void setFilePathRoot(String filePathRoot) {
		this.filePathRoot = filePathRoot;
	}
}
